package com.eastfair.transport.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.eastfair.core.base.R;
import com.eastfair.core.exception.BizException;
import com.eastfair.transport.service.TransportServiceProviderService;
import com.eastfair.venuebooking.api.ServiceProviderFeign;
import com.eastfair.venueservice.dto.ServiceProviderDTO;
import com.eastfair.venueservice.enumeration.ServiceProviderTypeEnum;
import com.eastfair.venueservice.vo.ServiceProviderVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * <p>
 * 业务实现类
 * 配套服务商信息
 * </p>
 *
 * @author lyw
 * @date 2022 -07-15
 */
@Slf4j
@Service
public class TransportServiceProviderServiceImpl implements TransportServiceProviderService {

    @Resource
    private ServiceProviderFeign serviceProviderFeign;

    @Override
    public ServiceProviderVO getServiceProviderByUserId(Long userId) {
        log.info("HomeServiceProviderServiceImpl +++++ getHomeServiceProviderByUserId - 查询主场服务商信息 params={}", userId);
        R<ServiceProviderVO> r = serviceProviderFeign.getByUserId(userId, ServiceProviderTypeEnum.MAIN_TRANSPORT_SERVICE_PROVIDER.getCode());
        if (!r.getIsSuccess()) {
            throw BizException.wrap(r.getCode(), r.getMsg());
        }
        return r.getData();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean saveServiceProvider(ServiceProviderDTO serviceProviderDTO) {
        log.info("HomeServiceProviderServiceImpl +++++ saveHomeServiceProvider - 保存主场服务商信息 params={}", JSONObject.toJSON(serviceProviderDTO));
        serviceProviderDTO.setServiceProviderType(ServiceProviderTypeEnum.MAIN_TRANSPORT_SERVICE_PROVIDER.getCode());
        R<Boolean> r = serviceProviderFeign.submitServiceProvider(serviceProviderDTO);
        if (!r.getIsSuccess()) {
            throw BizException.wrap(r.getCode(), r.getMsg());
        }

        return r.getData();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateServiceProvider(ServiceProviderDTO serviceProviderDTO) {
        log.info("HomeServiceProviderServiceImpl +++++ updateHomeServiceProvider - 修改主场服务商信息 params={}", JSONObject.toJSON(serviceProviderDTO));
        serviceProviderDTO.setServiceProviderType(ServiceProviderTypeEnum.MAIN_TRANSPORT_SERVICE_PROVIDER.getCode());
        R<Boolean> r = serviceProviderFeign.submitServiceProvider(serviceProviderDTO);
        if (!r.getIsSuccess()) {
            throw BizException.wrap(r.getCode(), r.getMsg());
        }
        return r.getData();
    }

}
